package callable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author yunfei.shang
 * @title
 * @date 2019/07/10 15:40
 * @descreption
 */
public class CallableThread {
    public static void main(String[] args) {
        Callable<Integer> callable = () -> {
            System.out.println("开始启动线程");
            Thread.sleep(1000);
            System.out.println("线程执行完成");
            return 1;
        };
        ExecutorService executorService = new ThreadPoolExecutor(2, 2, 1L, TimeUnit.HOURS, new LinkedBlockingDeque<>());
        List<Future<Integer>> futureList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            futureList.add(executorService.submit(callable));
        }
        try {
            for (Future<Integer> result : futureList) {
                System.out.println(result.get());
            }
        } catch (Exception ex) {
        }
        executorService.shutdown();
    }
}
